/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import dbs.Friend;
import dbs.User;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 */
@Stateless
public class FriendFacade extends AbstractFacade<Friend> {
    @PersistenceContext(unitName = "OurConnectionServer-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public FriendFacade() {
        super(Friend.class);
    }
    
    public Friend findFriendData(long userId, long friendId)
    {
        TypedQuery<Friend> query = em.createNamedQuery("Friend.findByPK", Friend.class)
                .setParameter("userId", userId)
                .setParameter("friendId", friendId);
        
        for(Friend friend : query.getResultList())
        {
            return friend;
        }
        
        // not exist
        return null;
    }
    
    public List<Friend> findFriendDataList(long userId)
    {
        TypedQuery<Friend> query = em.createNamedQuery("Friend.findByUserId", Friend.class)
                .setParameter("userId", userId);
        
        return query.getResultList();
    }
    
    public List<Friend> findFollowerFriendList(long userId)
    {
        TypedQuery<Friend> query = em.createNamedQuery("Friend.findByUserIdAndFollwerFlag", Friend.class)
                .setParameter("userId", userId)
                .setParameter("followerFlag", 1);
        
        return query.getResultList();
    }
    
}
